package com.yuews.codegenerator.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 表结构信息模型
 */
public class TableInfo {
    private String tableName;         // 表名
    private String tableComment;      // 表注释
    private List<ColumnInfo> columns; // 字段列表
    private String primaryKey;        // 主键字段名
    private List<String> indexes;     // 索引列表
    private String entityName;        // 自定义实体类名（可选）

    public TableInfo() {
        this.columns = new ArrayList<>();
        this.indexes = new ArrayList<>();
    }

    // Getters and Setters
    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public List<ColumnInfo> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnInfo> columns) {
        this.columns = columns;
    }

    public void addColumn(ColumnInfo column) {
        this.columns.add(column);
    }

    public String getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(String primaryKey) {
        this.primaryKey = primaryKey;
    }

    public List<String> getIndexes() {
        return indexes;
    }

    public void setIndexes(List<String> indexes) {
        this.indexes = indexes;
    }

    public void addIndex(String index) {
        this.indexes.add(index);
    }

    /**
     * 获取实体类名称
     * 如果设置了自定义实体类名，则使用自定义名称
     * 否则使用首字母大写的驼峰命名
     */
    public String getEntityName() {
        if (entityName != null && !entityName.isEmpty()) {
            return entityName;
        }
        return underlineToCamelCase(tableName, true);
    }
    
    /**
     * 设置自定义实体类名
     */
    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    /**
     * 获取驼峰命名（首字母小写）
     */
    public String getCamelCaseName() {
        return underlineToCamelCase(tableName, false);
    }

    /**
     * 下划线转驼峰命名
     */
    private String underlineToCamelCase(String str, boolean capitalizeFirst) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = capitalizeFirst;
        
        for (char c : str.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    result.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }
        
        return result.toString();
    }
}